/*
 * @(#)HeaderHandleInterceptor.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.interceptor.impl;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.nhncorp.lucy.web.helper.ServletHelper;
import com.nhncorp.lucy.web.interceptor.AbstractInterceptor;
import com.nhncorp.usf.core.xwork.ActionInvocationHelper;

import com.opensymphony.xwork.ActionInvocation;

/**
 * inject Header data of request to {@link Map}.
 *
 * @author Web Platform Development Team
 */
public class HeaderHandleInterceptor extends AbstractInterceptor implements InterceptorPropertyAware {
    private static final long serialVersionUID = 4236914684598247180L;

    Log log = LogFactory.getLog(HeaderHandleInterceptor.class);

    private Properties properties;

    /**
     * @see com.nhncorp.usf.core.interceptor.impl.InterceptorPropertyAware#setProperties(java.util.Properties)
     * {@inheritDoc}
     */
    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    /**
     * @param invocation the ActionInvocation
     * @return the interceptor result
     * @throws Exception the Exception
     */
    @SuppressWarnings("unchecked")
	public String intercept(ActionInvocation invocation) throws Exception {
        if (properties == null || properties.size() == 0) {
            log.debug("property is blank. execute action invocation.");
            return invocation.invoke();
        }

        Map<String, Object> map = new HashMap<String, Object>();
        HttpServletRequest request = ServletHelper.getRequest(invocation);
        String headerValue = (String)properties.get("requestHeader");

        if ("all".equals(headerValue)) {
            Enumeration enu = request.getHeaderNames();

            while (enu.hasMoreElements()) {
                String headerName = (String)enu.nextElement();
                map.put(headerName, request.getHeader(headerName));
            }

        } else {
            StringTokenizer token = new StringTokenizer(headerValue, ",");

            while (token.hasMoreTokens()) {
                String headerName = token.nextToken();
                map.put(headerName, request.getHeader(headerName));
            }
        }

        ActionInvocationHelper.getDataMap(invocation).put("requestHeader", map);

        return invocation.invoke();
    }
}

/* 특정 필드를 원하면 아래와 같이 등록한다.
 <interceptors>
   <usf:interceptor name="headerHandle" class="com.nhncorp.usf.core.interceptor.impl.HeaderHandleInterceptor"/>
	<interceptor-ref name="headerHandle">
		<parameter name="name"><![CDATA["requestHeader"]]></parameter>
		<parameter name="values"><![CDATA["referer,cookie"]]></parameter>
	</interceptor-ref>
</interceptors>
모든 필드를 등록하고자할때는 아래와 같이 한다.
 <interceptors>
   <usf:interceptor name="headerHandle" class="com.nhncorp.usf.core.interceptor.impl.HeaderHandleInterceptor"/>
	<interceptor-ref name="headerHandle">
		<parameter name="name"><![CDATA["requestHeader"]]></parameter>
		<parameter name="value"><![CDATA["all"]]></parameter>
	</interceptor-ref>
</interceptors>

name에 설정된 parameter에  values에 설정된 header의 특정값을 넣는다. 
위의 sample은 parameter에 requestHeader라는 이름으로 header의 특정값을 넣게 되므로
첫번째 예제 같은 경우에는 run.xml에서 아래와 같이 사용할 수 있다.
<bo id="com.nhncorp.usf.todo.bo.TodoBO">
	<method id="methodID" return-id="methodReturnId">
		<param id="parameterName" ref="requestHeader.referer" />
	</method>
</bo>
*
*/